package com.nt.train.controller;
import com.nt.train.entity.DictType;
import com.nt.train.service.DictTypeService;
import com.nt.train.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * 模拟开发字典增删改查列表处理
 * @author hlf
 * @version 1.0
 * @date 2022/8/17 17:16
 */
@RestController
@RequestMapping("/dictType")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    @PostMapping(value = "/listDictTypePage/{pageIndex}/{pageSize}")
    public AjaxResult listDictTypePage(@PathVariable Integer pageIndex,@PathVariable Integer pageSize, @RequestBody  DictType dictType) {
        List<DictType>  pageList = dictTypeService.listDictTypePage(pageIndex,pageSize,dictType);
        return AjaxResult.success(pageList);
    }

    @PostMapping(value = "/findList")
    public AjaxResult findList(@RequestBody  DictType dictType) {
        List<DictType>  pageList = dictTypeService.selectDictTypeAll();
        return AjaxResult.success(pageList);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return AjaxResult.success(dictTypeService.selectDictTypeById(id));
    }

    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody DictType dictType) {
        DictType d = dictTypeService.getByDictName(dictType.getDictName());
        if (d != null) {
            return AjaxResult.error("字典类型名称({dictName})已被使用".replace("{dictName}", dictType.getDictName()));
        }
        dictTypeService.insertDictType(dictType);
        return AjaxResult.success();
    }

    @PostMapping(value = "/edit")
    public AjaxResult edit(@RequestBody DictType dictType) {
        DictType d = dictTypeService.getByDictName(dictType.getDictName());
        if (d != null) {
            return AjaxResult.error("字典类型名称({dictName})已被使用".replace("{dictName}", dictType.getDictName()));
        }
        dictTypeService.updateDictType(dictType);
        return AjaxResult.success();
    }

    @PostMapping(value = "/del/{id}")
    public AjaxResult del(@PathVariable Long id) {
        dictTypeService.deleteDictTypeById(id);
        return AjaxResult.success();
    }

    @PostMapping(value = "/batchdel")
    public AjaxResult batchdel(@RequestBody List<Long> ids) {
        dictTypeService.deleteDictTypeByIds(ids);
        return AjaxResult.success();
    }

    //http://localhost:8080/DictType/batchdel2?ids=1,2,3
    @PostMapping(value = "/batchdel2")
    public AjaxResult batchdel2(@RequestParam("ids") List<Long> ids) {
        dictTypeService.deleteDictTypeByIds(ids);
        return AjaxResult.success();
    }

    //http://localhost:8080/DictType/batchdel2?ids=1,2,3
    @PostMapping(value = "/batchdel3")
    public AjaxResult batchdel3(Long[] ids) {
        //dictTypeService.deleteDictTypeByIds(ids);
        System.out.println(ids);
        return AjaxResult.success();
    }
}
